package com.aaa.marketresearch.android.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.Button;
import android.widget.FrameLayout;
import com.aaa.marketresearch.android.app.R;
import com.aaa.marketresearch.android.data.UserInfo;
import com.aaa.marketresearch.android.data.domain.FamilyMember;
import com.aaa.marketresearch.android.data.domain.Market;
import com.aaa.marketresearch.android.data.domain.Offer;


/**
 * @author Nail
 */

public class NavigationMenu extends FrameLayout {

    private Context context;

    private Button familyMemberButton;
    private Button marketButton;
    private Button offerButton;
    private Button billButton;


    public NavigationMenu(Context context) {
        this(context, null);
    }

    public NavigationMenu(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.navigation_menu_view, this, true);

        this.familyMemberButton = (Button) findViewById(R.id.familyMemberButton);
        this.marketButton = (Button) findViewById(R.id.marketButton);
        this.offerButton = (Button) findViewById(R.id.offerButton);
        this.billButton = (Button) findViewById(R.id.billButton);

    }

    public void reload() {
        UserInfo userInfo = UserInfo.getInstance();
        Offer offer = userInfo.getCurrentBillContent().getOffer();
        Market market = userInfo.getCurrentMarket();
        FamilyMember familyMember = userInfo.getCurrentFamilyMember();
        if (familyMember == null) {
            this.familyMemberButton.setEnabled(false);
            this.familyMemberButton.setText("кто");
        } else {
            this.familyMemberButton.setEnabled(true);
            this.familyMemberButton.setText(cut(familyMember.getName()));
        }
        if (market == null) {
            this.marketButton.setEnabled(false);
            this.marketButton.setText("где");
        } else {
            this.marketButton.setEnabled(true);
            this.marketButton.setText(cut(market.getName()));
        }
        if (offer == null) {
            this.offerButton.setEnabled(false);
            this.offerButton.setText("что");
        } else {
            this.offerButton.setEnabled(true);
            this.offerButton.setText(cut(offer.getName()));
        }
        if (userInfo.getBill().size() == 0) {
            this.billButton.setEnabled(false);
        } else {
            this.billButton.setEnabled(true);
        }
    }


    public void hideFamilyMember() {
        this.familyMemberButton.setEnabled(false);
    }

    public void hideMarket() {
        this.marketButton.setEnabled(false);
    }

    public void hideOffer() {
        this.offerButton.setEnabled(false);
    }

    private String cut(String text) {
        if (text.length() > 10) {
            return text.substring(0, 10);
        } else {
            return text;
        }
    }

}